﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.Base.Util;
using FastExecutor.Message.Application;
using FastExecutor.ORG.DB;
using FastExecutor.ORG.Entity;
using FastExecutor.ORG.Enum;
using FastExecutor.ORG.Util;
using FastIOC.Annotation;
using FastUtil.Base;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("Auth")]
    public class AuthController : BaseController
    {
        [AutoWired]
        private DB_User @DB_User;
        [AutoWired]
        private DB_Config @DB_Config;
        [AutoWired]
        private AuthUtil @AuthUtil;

        [HttpPost, Route("Login")]
        public ActionResult Login()
        {
            JObject Params = GetParams();
            ReturnValue result = new ReturnValue();
            Frame_LoginLog LoginLog = new Frame_LoginLog();
            Frame_User UserEntity = null; ;
            bool Flag = true;
            string msg = string.Empty;
            string LoginID = Convert.ToString(Params.GetValue("LoginID"));
            string PassWord = EncryptUtil.GetFromBase64String(Convert.ToString(Params.GetValue("PassWord")));
            try
            {
                UserEntity = @DB_User.FindOne_LoginID(LoginID);
                LoginLog.LoginName = LoginID;
                if (UserEntity == null)
                {
                    Flag = false;
                    msg = "用户不存在";
                    LoginLog.LoginResult = "用户不存在";
                }
                else if (UserEntity.PassWord != EncryptUtil.MD5Encrypt(LoginID + "|" + PassWord))
                {
                    Flag = false;
                    msg = "密码不正确";
                    LoginLog.LoginResult = "密码不正确";
                }
            }
            catch (Exception e)
            {
                LoginLog.LoginResult = e.Message;
                Flag = false;
            }

            if (!Flag)
            {
                result.Add("code", -100);
                result.Add("msg", msg);
                msg = "登陆过程中发生一些错误，请联系管理员";
            }
            else
            {
                LoginLog.LoginResult = "登陆成功";
                string Token = @AuthUtil.CreateTokenKey(UserEntity.RowGuid);
                result.Add("token", Token);
                result.Add("returnurl", CommonUtil.GetRootPath() + "Frame/Home/Index");
                AuthUtil.SetTokenCookie(Request, Response, Token);
            }

            LoginLog.RowGuid = CommonUtil.CreateCommonGuid();
            LoginLog.LoginDate = DateTime.Now;
            LoginLog.FromIP = CommonUtil.GetClientIP();
            MessageCenter.GetInstance().SendMessage(FrameEnum.LogType.Login.ToString(), LoginLog);
            return result;
        }

        [HttpPost, Route("LoginOut")]
        public ActionResult LoginOut()
        {
            @AuthUtil.RemoveAuth(Request, Response);
            ReturnValue result = new ReturnValue();
            result.Add("returnurl", CommonUtil.GetRootPath() + "Frame/Home/Login");
            return result;
        }
    }
}
